<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%>

<script type="text/javascript">
var prod_opera; //action url
//卡类型数据源
var dimen_store = new Ext.data.JsonStore({
	root: "data",
	totalProperty: "total",
	fields : [ "field_type", "field", "field_name"],
	url : "dimnsion!dimenGridList.action"
});
function thisrequest(params){
	Ext.Ajax.request({
		clientValidation: true,
		params : params,
		url: prod_opera,
		success: function(response, opts){
			var d=eval('('+response.responseText+')');
			if(d.data){
				Ext.getCmp('p1').getBottomToolbar().doRefresh();
				Ext.getCmp("mycontainer").layout.setActiveItem(0);
				Ext.Msg.show({
					title:"提示",
					msg:"操作成功!",
					buttons:Ext.Msg.OK,
					icon: Ext.MessageBox.INFO
				});		
				
				Ext.getCmp("save").setDisabled(false);
			} else {
				Ext.Msg.show({
					title:"提示",
					msg:"操作失败!",
					buttons:Ext.Msg.OK,
					icon: Ext.MessageBox.ERROR
				});
			}
		}
	});
};
//定义数据显示面板
var p1 = {
	id: "p1",
	xtype: "grid",
	stripeRows:true,
	loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
	autoExpandColumn: "cn_name",
	autoExpandMin : 100,//自动填充最小宽度
	store: dimen_store,
	headersDisabled: true,
	columns:[
       	new Ext.grid.RowNumberer({header: "序号", width: 50}),
  		{header: "维度类型编号",	dataIndex: "field_type", menuDisabled: true},
  		{header: "字段名",		dataIndex: "field", menuDisabled: true},
  		{header: "字段中文名称",	dataIndex: "field_name",id:"cn_name", menuDisabled: true}
	],
	buttonAlign: "center",
	bbar: new Ext.ux.PagingToolbar({
       	pageSize: limit,
       	store: dimen_store
    }),
	buttons:[{
			text : "新增",
			handler : function() {
				prod_opera = "dimnsion!addDimension.action";
				Ext.getCmp("p2").resetParam();
				Ext.getCmp("mycontainer").layout.setActiveItem(1);
			}
		},{
			text : "修改",
			handler : function() {
				prod_opera = "dimnsion!editDimension.action";
				var record = Ext.getCmp("p1").getSelectionModel().getSelected();
				if (record){
					Ext.getCmp("p2").setParam(record.data);
					Ext.getCmp("mycontainer").layout.setActiveItem(1);
				} else {
					Ext.Msg.show({
						title:"警告",
						msg: "没有选中任何行数据，无法进行此操作!",
						buttons: Ext.Msg.OK,
						animEl: "elId",
						icon:Ext.MessageBox.WARNING
					});
				}
			}
		},{
			text : "删除",
			handler : function() {
				prod_opera = "dimnsion!deleteDimension.action";
				var p1 = this.ownerCt.ownerCt;
				var record = p1.getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm("警告","确定要删除吗?", function(btn){
					    if (btn == "yes"){
					    	Ext.Ajax.request( {
					    		url : prod_opera,
					    		params : {"dimen.field_type":record.data.field_type},
					    		success: function(response, opts){
									var d=eval("("+response.responseText+")");
									if(d.data){
										Ext.getCmp("p1").getBottomToolbar().doRefresh();
										Ext.getCmp("mycontainer").layout.setActiveItem(0);
										Ext.Msg.show({
											title:"提示",
											msg:"操作成功!",
											buttons:Ext.Msg.OK,
											icon: Ext.MessageBox.INFO
										});
									} else {
										Ext.Msg.show({
											title:"提示",
											msg:"操作失败!",
											buttons:Ext.Msg.OK,
											icon: Ext.MessageBox.ERROR
										});
									}
								}
					    	});
					    }
					});									
				}
				else
				{
				   Ext.Msg.show({
						title:"警告",
						msg: "未选中需要删除的信息!",
						buttons: Ext.Msg.OK,
						animEl: "elId",
						icon:Ext.MessageBox.WARNING
					});
				}
			}
		}]
};
		
		var p2 = {
			id: "p2",
		    labelWidth: 125,
			xtype: "form",
		    bodyStyle:"padding:5px 5px 0",
		    width: 350,
			defaultType: "textfield",
			items: [
				{id: "field_type",allowBlank: false,fieldLabel: "维度类型编号",xtype:"numberfield",maxLength:5,	name: "field_type",	anchor: "100%"},
				{id: "fields",	allowBlank: false,fieldLabel: "字段名",vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项!";
		        		if(Ext.fucCheckCHLength(val)>20){
		        			return "请输入20个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
		        		}
		        		return true;
		        	},	name: "field",		anchor: "100%"},
				{id: "field_name",	allowBlank: false,fieldLabel: "字段中文名称", vtype:"alphanumchinese",
					validator:function (val){
		        		if(val=="")return "该输入项为必输项!";
		        		if(Ext.fucCheckCHLength(val)>20){
		        			return "请输入20个字符范围内的值!<br>注：一个中文字符算两个字符的长度!";
		        		}
		        		return true;
		        },name: "field_name",	anchor: "100%"}
			],
			buttonAlign: "center",
			buttons:[{
				text : "保存",
				id:"save",
				handler : function(e) {
					if(!Ext.getCmp("p2").getForm().isValid()){
						Ext.Msg.show({
							title:"警告",
							msg: "验证不通过!",
							buttons: Ext.Msg.OK,
							animEl: "elId",
							icon:Ext.MessageBox.WARNING
						});
						return;
					}
							
					e.setDisabled(true);
					var params={
							'dimen.field_type':Ext.getCmp("field_type").getValue(),
							'dimen.field':Ext.getCmp('fields').getValue(),
							'dimen.field_name':Ext.getCmp('field_name').getValue()
					};
					if(prod_opera == "dimnsion!addDimension.action"){
						Ext.Ajax.request({
							clientValidation: true,
							params: {'dimen.field_type':Ext.getCmp('field_type').getValue()},
							url: 'dimnsion!validtateField_type.action',
							success: function(response,opts){
								var res = eval('('+response.responseText+')');
								if(res.code=="0"){
									thisrequest(params);
								}else{
									Ext.Msg.show({
										title: '提示',
										msg: '输入的维度类型编号已经存在！',
										button: Ext.Msg.OK,
										icon: Ext.MessageBox.ERROR
									});
									
								}
								Ext.getCmp("save").setDisabled(false);
							}
						});
					}else{
						thisrequest(params);
					}
				}
			},
			{
				text : "取消",
				handler : function() {
					Ext.getCmp("mycontainer").layout.setActiveItem(0);
				}
			}],
			setParam: function(data){
				Ext.getCmp("field_type").setValue(data.field_type);
				Ext.getCmp("field_type").setDisabled(true);
				Ext.getCmp("fields").setValue(data.field);
				Ext.getCmp("field_name").setValue(data.field_name);
			},
			resetParam: function(){
				Ext.getCmp("field_type").setValue("");
				Ext.getCmp("field_type").setDisabled(false);
				Ext.getCmp("fields").setValue("");
				Ext.getCmp("field_name").setValue("");
			}
		};
		
		var dimen_pel = new Ext.Panel({
			id: "mycontainer", 
			border : false,		
			layout: "card",
			activeItem: 0,
			items: [p1, p2]
		});
		
		dimen_store.load({params:{start : 0,limit : limit}});
		
		var CenterPanel=Ext.getCmp("CenterPanel");
		CenterPanel.removeAll();
		CenterPanel.add(dimen_pel);
		CenterPanel.doLayout();
	</script>
